package com.fany.fastsearcher.activity;

import android.graphics.PixelFormat;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.WindowManager;
import com.fany.fastsearcher.R;
import com.fany.utils.FileHelper;
import com.fany.views.rotatemenu.RotateMenuItem;
import com.fany.views.rotatemenu.RotateMenuView;

import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.List;

/**
 * Created by FANY on 2015/1/29.
 */
public class FileSearchRotateMenuController
{
    private static final int ShowFileKindsCommandId = 0x2000;
    private static final int SearchPicturesCommandId = 0x2002;
    private static final int SearchMusicsCommandId = 0x2003;
    private static final int SearchVideosCommandId = 0x2004;
    private static final int SearchDocumentsCommandId = 0x2005;
    private static final int SearchApplicationsCommandId = 0x2006;
    private static final int SearchCompressedCommandId = 0x2007;
    private static final int SearchConfigureCommandId = 0x2008;
    private static final int SettingCommandId = 0x2100;
    private static final int RefreshFilesCommandId = 0x2200;

    private static final int SearchFileCommandId = 0x10000;
    private RotateMenuView _rotateView;
    private FileSearchActivity _activity;
    private HashMap<Integer, RotateMenuItem> _menus = new HashMap<Integer, RotateMenuItem>();

    public static FileSearchRotateMenuController loadRotateMenuController(FileSearchActivity activity)
    {
        return new FileSearchRotateMenuController(activity);
    }

    private FileSearchRotateMenuController(FileSearchActivity activity)
    {
        _activity = activity;
        createRotateMenuView();
    }

    private RotateMenuItem addItem(String Text, int resourceId, int commandId)
    {
        RotateMenuItem item = _rotateView.addItem(Text, resourceId, commandId);
        _menus.put(commandId, item);
        return item;
    }

    private RotateMenuItem addChildItem(RotateMenuItem parent, String Text, int resourceId, int commandId)
    {
        RotateMenuItem item = _rotateView.addChildItem(parent, Text, resourceId, commandId);
        _menus.put(commandId, item);
        return item;
    }

    private void addFileKindChildItem(int commandId)
    {
        List<String> extensions = null;
        switch (commandId)
        {
            case SearchMusicsCommandId:
                extensions = FileHelper.getExtensionsByFileKind(FileHelper.FileKind.Music);
                break;
            case SearchVideosCommandId:
                extensions = FileHelper.getExtensionsByFileKind(FileHelper.FileKind.Video);
                break;
            case SearchDocumentsCommandId:
                extensions = FileHelper.getExtensionsByFileKind(FileHelper.FileKind.Document);
                break;
            case SearchCompressedCommandId:
                extensions = FileHelper.getExtensionsByFileKind(FileHelper.FileKind.Compressed);
                break;
            case SearchConfigureCommandId:
                extensions = FileHelper.getExtensionsByFileKind(FileHelper.FileKind.Configure);
                break;
            case SearchPicturesCommandId:
                extensions = FileHelper.getExtensionsByFileKind(FileHelper.FileKind.Picture);
                break;
            default:
                return;
        }
        for (String extension : extensions)
        {
            int resourceId = FileHelper.getResourceIdByFileExtension(extension);
            RotateMenuItem item = _menus.get(commandId);
            item = addChildItem(item, "", resourceId, SearchFileCommandId + FileHelper.getUniqueIdByFileExtension(extension));
            item.extraData = extension;
        }
    }

    private void createRotateMenuView()
    {
        _rotateView = new RotateMenuView(_activity);
        WindowManager.LayoutParams windowManagerParams;
        windowManagerParams = new WindowManager.LayoutParams();
        windowManagerParams.type = WindowManager.LayoutParams.MATCH_PARENT;
        windowManagerParams.format = PixelFormat.RGBA_8888;
        windowManagerParams.gravity = Gravity.LEFT | Gravity.TOP;

        RotateMenuItem fileKindItem = addItem("", R.drawable.filetype, ShowFileKindsCommandId);

        addChildItem(fileKindItem, "", R.drawable.filekind_picture, SearchPicturesCommandId);
        addFileKindChildItem(SearchPicturesCommandId);

        addChildItem(fileKindItem, "", R.drawable.filekind_music, SearchMusicsCommandId);
        addFileKindChildItem(SearchMusicsCommandId);

        addChildItem(fileKindItem, "", R.drawable.filekind_video, SearchVideosCommandId);
        addFileKindChildItem(SearchVideosCommandId);

        addChildItem(fileKindItem, "", R.drawable.filekind_document, SearchDocumentsCommandId);
        addFileKindChildItem(SearchDocumentsCommandId);

        addChildItem(fileKindItem, "", R.drawable.filekind_application, SearchApplicationsCommandId);

        addChildItem(fileKindItem, "", R.drawable.filekind_compressed, SearchCompressedCommandId);
        addFileKindChildItem(SearchCompressedCommandId);

        addChildItem(fileKindItem, "", R.drawable.filekind_configure, SearchConfigureCommandId);
        addFileKindChildItem(SearchConfigureCommandId);

        RotateMenuItem item = _rotateView.addItem("", R.drawable.setting, SettingCommandId);
        _menus.put(SettingCommandId, item);
        item = _rotateView.addItem("", R.drawable.reload, RefreshFilesCommandId);
        _menus.put(RefreshFilesCommandId, item);

        _activity.addContentView(_rotateView, windowManagerParams);

        _handler = new CommandHandler(_activity, FileSearchRotateMenuController.this);
        new Thread(new CommandWatchThread()).start();

    }

    private Handler _handler;

    private static class CommandHandler extends Handler
    {
        private WeakReference<FileSearchActivity> _activity;
        private WeakReference<FileSearchRotateMenuController> _controller;

        public CommandHandler(FileSearchActivity activity, FileSearchRotateMenuController controller)
        {
            _activity = new WeakReference<FileSearchActivity>(activity);
            _controller = new WeakReference<FileSearchRotateMenuController>(controller);
        }

        public void handleMessage(Message msg)
        {
            FileSearchRotateMenuController controller = _controller.get();
            RotateMenuItem item = controller._menus.get(msg.what);
            switch (msg.what)
            {
                case ShowFileKindsCommandId:
                case SearchPicturesCommandId:
                case SearchMusicsCommandId:
                case SearchVideosCommandId:
                case SearchDocumentsCommandId:
                case SearchCompressedCommandId:
                case SearchConfigureCommandId:
                    controller._rotateView.hideChildItems(item.level + 1);
                    controller._rotateView.showChildItems(item);
                    break;
                case SearchApplicationsCommandId:
                    _activity.get().searchFiles(".apk");
                    controller._rotateView.hideChildItems(item.level);
                    controller._rotateView.hideChildItems(item.level + 1);
                    break;
                case RefreshFilesCommandId:
                    _activity.get().refreshFiles();
                    break;
                case SettingCommandId:
                    _activity.get().settings();
                    break;
            }
            if (msg.what >= SearchFileCommandId)
            {
                _activity.get().searchFiles(item.extraData.toString());
                controller._rotateView.hideChildItems(item.level);
                controller._rotateView.hideChildItems(item.parent.level);
            }
            super.handleMessage(msg);
        }
    }

    class CommandWatchThread implements Runnable
    {
        public void run()
        {
            while (!Thread.currentThread().isInterrupted())
            {
                RotateMenuItem item = _rotateView.getTouchedItem();
                if (item != null)
                {
                    Message message = new Message();
                    message.what = item.commandId;
                    _handler.sendMessage(message);
                }
                try
                {
                    _rotateView.cleanTouchedItem();
                    Thread.sleep(10);
                }
                catch (InterruptedException e)
                {
                    Thread.currentThread().interrupt();
                }
            }
        }
    }
}
